package com.pan.cart;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CartAnalysisDriver {
    public Boolean driver(String name) throws IOException, InterruptedException, ClassNotFoundException {
        //创建一个job对象
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://192.168.10.102:8020");
        Job job = Job.getInstance(configuration);
        //设置jar包路径
        job.setJarByClass(CartAnalysisDriver.class);
        //关联mapper类和reducer类
        job.setMapperClass(CartAnalysisMapper.class);
        job.setReducerClass(CartAnalysisReducer.class);
        //设置mapper的输出key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置最终的key和value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //设置输出和输入路径
        FileInputFormat.setInputPaths(job,new Path("/pan/"+name+".csv"));
        FileOutputFormat.setOutputPath(job,new Path("/out/"+name+"_analysis"));

        //提交job
        boolean result = job.waitForCompletion(true);
        return result;
    }
}
